<?php

/**
 * Description of problema
 *
 * @author Leonardo Lino
 */
class Problema extends CI_Controller {

    private $smarty;

    public function __construct() {
        parent::__construct();
        $this->load->library('session');
        $session_id = $this->session->userdata('session_id');
        require_once 'smartyLoader.php';
        $this->smarty = $smarty;
        $this->load->model('problemaModel');
        $this->user->isAccessBackOffice($this->smarty);
    }

    public function index() {

        $problemas = $this->problemaModel->loadAll();
        if ($problemas == null) {
            $this->smarty->assign("vazio", 0);
        } else {
            $this->smarty->assign("vazio", 1);
        }

       
        $this->smarty->assign("problemas", $problemas);
        $this->smarty->assign("admins", $this->user->getAdmins());
        $this->smarty->assign("templateActivo", "problema.tpl.html");
        $this->smarty->display('admin/header.tpl.html');
        $this->smarty->display('admin/main.tpl.html');
        $this->smarty->display('admin/footer.tpl.html');
    }
    
    public function novo() {
        
        if (isset($_POST['publicado']) && $_POST['publicado']=="ON"){
            $publicado = 1;
        } else {
            $publicado =0;
        }
        
        $state = $_POST['state'];
        if ($state == "1") {
            $resultado = $this->problemaModel->novo($_POST['id_autor'], $_POST['titulo'], $_POST['problema'], $_POST['resposta'], $_POST['sugestoes'], $publicado);
            $this->index();
        } else {
                                                        //($id, $id_autor, $titulo, $problema, $resposta, $sugestoes, $publicado)
            $resultado= $this->problemaModel->update($_POST['vid'], $_POST['id_autor'], $_POST['titulo'], $_POST['problema'], $_POST['resposta'], $_POST['sugestoes'], $publicado);
            $this->index();
        }
    }
    
    public function delete (){
      $resultado = $this->problemaModel->deleteFromId($_GET['vid']);
      $this->index();
    }
    
      public function loadAjax() {
        $row = $this->problemaModel->loadFromId($_POST['vid']);
        echo json_encode($row);
    }

}

?>
